//
//  AppDelegate.m
//  MyMovie
//
//  Created by Weiwei on 16/2/24.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "AppDelegate.h"
#import "CustomTabBarController.h"
#import "MoreTableViewController.h"
@interface AppDelegate (){
    UIScrollView* scrollView;
}

@end

#pragma -mark本节课目标
//1.第一次加载页面
//（1）scrollView
// (2)上面有个Button
// (3)当我们的程序运行过一次后不会出现



@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //1.构建window
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    //2.将window显示到屏幕的最前端
    [self.window makeKeyAndVisible];
    
#pragma -mark不导入头文件，用字符串创建一个类
    //（1）创建一个包含5个viewController类名的字符串数组
    NSArray* array = @[@"CinemaViewController",@"BuyViewController",@"HomeViewController",@"ChooseViewController"];
    
    //(2)创建一个可变数组，用于存放viewC
    NSMutableArray* viewCArray = [NSMutableArray arrayWithCapacity:1];
    
    //(3)for in循环创建控制器
    for (NSString* string in array) {
        
      //通过class（类名）来创建一个对象
      BaseViewController* viewC = [[NSClassFromString(string) alloc]init];
        
      //创建一个导航控制器
//        UINavigationController* navC = [[UINavigationController alloc]initWithRootViewController:viewC];
        
        BaseNavigationController* navC = [[BaseNavigationController alloc]initWithRootViewController:viewC];
        
      //将导航控制器存放到数组中
        [viewCArray addObject:navC];
        
        
    }
#pragma -mark通过stroyBoard加载静态单元格页面
    //1.获取story
    UIStoryboard* story = [UIStoryboard storyboardWithName:@"MoreStoryboard" bundle:[NSBundle mainBundle]];
    //2.从storyBoard里获取带有标记的内容
     MoreTableViewController* moreVC = [story instantiateInitialViewController];
    
    //3.加到导航栏上
    BaseNavigationController* baseNC = [[BaseNavigationController alloc]initWithRootViewController:moreVC];
    
    //4.加到数组中，作为tabbar的子控制器
    [viewCArray addObject:baseNC];
    
    
    
    
    
    //(4)给自定义tabBarC设置子控制器
    CustomTabBarController* tabBarC = [[CustomTabBarController alloc]init];
    tabBarC.viewControllers = viewCArray;
    
    
#pragma -mark优化
    //设置默认的viewC
    tabBarC.selectedIndex = 2;
    
    
    
    //设置window的根控制器
    self.window.rootViewController = tabBarC;
    
    
    
    [self loadFirst];
    
    

    return YES;
}

#pragma -mark 步骤一 不管加载几次都遇到scrollView

-(void)loadFirst{
    
#pragma -mark 单里类
    //上来查找相应的key值对象存不存在
    NSNumber* isFirst = [[NSUserDefaults standardUserDefaults]objectForKey:@"isFirst"];
    
    if ([isFirst boolValue] != YES) {
    
    //1.创建一个srollView
    scrollView = [[UIScrollView alloc]initWithFrame:self.window.bounds];
    
    scrollView.pagingEnabled = YES;
    
    //2.不显示水平滑动条
    scrollView.showsHorizontalScrollIndicator = NO;
    
    //3.设置内容大小
    scrollView.contentSize =CGSizeMake(3*KScreenW, KScreenH);
    
    //4.当滑动超出区域，不能滑动
    scrollView.bounces = NO ;
    
    
    [self.window addSubview:scrollView];
    
    
    
    //1.创建三个imageView
    UIImageView* imageView1 = [[UIImageView alloc]initWithFrame:self.window.bounds];
    imageView1.image = [UIImage imageNamed:@"wizard1_920.jpg"];
    [scrollView addSubview:imageView1];
    
    
    UIImageView* imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenW, 0, KScreenW, KScreenH)];
    imageView2.image = [UIImage imageNamed:@"wizard2_920.jpg"];
    [scrollView addSubview:imageView2];
    
    
    UIImageView* imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(2*KScreenW, 0, KScreenW, KScreenH)];
    imageView3.image = [UIImage imageNamed:@"wizard3_920.jpg"];
    imageView3.userInteractionEnabled = YES;
    [scrollView addSubview:imageView3];
    
    
    //给imageView3加一个button
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(240, 570,KScreenW - 240, 100)];
    [imageView3 addSubview:button];
    [button addTarget:self action:@selector(dissMiss) forControlEvents:UIControlEventTouchUpInside];
   
        
     //加载一次scrollView后，存的isfirst变成1
        [[NSUserDefaults standardUserDefaults]setObject:@1 forKey:@"isFirst"];
        
        
        
    }
    
}
-(void)dissMiss{

    
    [scrollView removeFromSuperview];
    
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
